package com.rs.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import com.rs.Settings;

/**
 * Anti Flood
 * 
 * @Author Apache Ah64
 */
public final class AntiFlood {

	private static ArrayList<String> connections = new ArrayList<String>(Settings.PLAYERS_LIMIT * 2);

	public static void add(String ip) {
		connections.add(ip);
	}

	public static void remove(String ip) {
		connections.remove(ip);
	}

	public static int getSessionsIP(String ip) {
		int amount = 1;
		for (int i = 1; i < connections.size(); i++) {
			if (connections.get(i).equalsIgnoreCase(ip))
				amount++;
		}

		return amount;
	}
	
	public static void getLocation(String ip) {
		try{ 
		URL url = new URL("http://freegeoip.net/csv/" + ip);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.connect();

		InputStream is = connection.getInputStream();

		int status = connection.getResponseCode();
		if (status != 200) {
		    return;
		}

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		for (String line; (line = reader.readLine()) != null;) {
			
		}
	} catch (Throwable e){
		Logger.handle(e);
	}
		
	}
}